<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\BaseController;
use App\Models\UserIdentity;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class IdentityController extends BaseController
{
    public function index(Request $request)
    {
        $param = $this->validate($request,[
            'identity' => 'required|in:1,2,3',
            'firm_profession_id' => 'integer',
            'keyword' => 'string|max:100'
        ]);

        $query = UserIdentity::query()
            ->selectRaw('user_identities.*,(select count(user_id) from identity_consultations where identity_id=user_identities.id) as consultation_count')
                    ->with(['category:id,name','auth:user_id,name','user:id,nickname,avatar,auth'])
                    ->where($param)
                    //->where('audit_status',2)
                    ->orderByDesc('view_count')
                    ->orderByDesc('id');

        if (isset($param['firm_profession_id'])){
            $query->where('firm_profession_id',$param['firm_profession_id']);
        }


        if (isset($params['keyword'])){
            $keyword = $params['keyword'];
            $query->whereHas('auth',function ($q) use ($keyword){
                $q->where('name','like','%'.$keyword.'%');
            });
        }



        $data = $query->simplePaginate();

        return $this->success($data);
    }


    public function show(Request $request)
    {
        $param = $this->validate($request,[
            'user_identity_id' => 'integer',
            'user_id' => 'integer'
        ]);
        $query = UserIdentity::query()
                    ->selectRaw('user_identities.*,(select count(user_id) from identity_consultations where identity_id=user_identities.id) as consultation_count');
        if (isset($param['user_id'])){
            $query->where('user_id',$param['user_id']);
        }else{
            $query->where('id',$param['user_identity_id']);
        }


        $data =  $query->with(['category:id,name','auth:user_id,name','user:id,nickname,avatar,auth'])
                    ->first();

        $data->view_count+=1;
        $data->save();

        return $this->success($data);

    }


    public function store(Request $request)
    {
        $param = $this->validate($request,[
            'avatar' => 'required',
            'identity' => 'required|in:1,2,3',
            'firm_profession_id' => 'required|integer',
            'certificate_photos' => 'array',
            'intro' => 'required',
            'record' => 'max:1000'
        ]);

        $user_id = auth('api')->id();

        $exists = UserIdentity::query()->where('user_id',$user_id)->exists();

        if ($exists){
            $param['audit_status'] = 1;
            $result =  UserIdentity::query()->where('user_id',$user_id)->update($param);
           return $result ? $this->success() : $this->error();
        }else{
            $param['user_id'] = $user_id;
            $identity = UserIdentity::query()->create($param);
            if ($identity->getKey()){
               return  $this->success();
            }
           return  $this->error();
        }
    }


    public function consultation(Request $request)
    {
        $params = $this->validate($request,[
            'identity_id' => 'required|integer'
        ]);


        $params['user_id'] = auth('api')->id();

        $exists = DB::table('identity_consultations')->where($params)->exists();

        if (!$exists){
            DB::table('identity_consultations')->insert($params);
        }

        return $this->success();

    }
}
